package com.xxh.myapp.controller;



import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.api.ApiController;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sun.net.httpserver.Authenticator;
import com.xxh.myapp.entity.Course;
import com.xxh.myapp.entity.Score;
import com.xxh.myapp.entity.Student;
import com.xxh.myapp.entity.Teacher;
import com.xxh.myapp.entity.wrapper.StudentWrapper;
import com.xxh.myapp.msg.Message;
import com.xxh.myapp.msg.ReturnMsg;
import com.xxh.myapp.service.*;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * (Teacher)表控制层
 *
 * @author makejava
 * @since 2020-05-01 16:38:29
 */
@RestController
@RequestMapping("teacher")
public class TeacherController extends ApiController {
    /**
     * 服务对象
     */
    @Resource
    private TeacherService teacherService;

    @Resource
    private StudentService studentService;
    @Resource
    private DeptService deptService;
    @Resource
    private StuclService stuclService;
    @Resource
    private ScoreService scoreService;
    @Resource
    private CourseService courseService;

    /**
     * 分页查询所有数据
     *
     * @param page 分页对象
     * @param teacher 查询实体
     * @return 所有数据
     */
    @GetMapping
    public R selectAll(Page<Teacher> page, Teacher teacher) {
        return success(this.teacherService.page(page, new QueryWrapper<>(teacher)));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public R selectOne(@PathVariable Serializable id) {
        return success(this.teacherService.getById(id));
    }

    /**
     * 新增数据
     *
     * @param teacher 实体对象
     * @return 新增结果
     */
    @PostMapping
    public R insert(@RequestBody Teacher teacher) {

        return success(this.teacherService.save(teacher));
    }

    /**
     * 修改数据
     *
     * @param teacher 实体对象
     * @return 修改结果
     */
    @PutMapping
    public R update(@RequestBody Teacher teacher) {
        return success(this.teacherService.updateById(teacher));
    }

    /**
     * 删除数据
     *
     * @param idList 主键结合
     * @return 删除结果
     */
    @DeleteMapping
    public R delete(@RequestParam("idList") List<Long> idList) {
        return success(this.teacherService.removeByIds(idList));
    }

    //根据课程号获取已经选修该课程的学生成绩(查询成绩表中的学生)
    @GetMapping("getStudentByCno")
    public Map<String,List<StudentWrapper>> getStudentByCno(int cno){
        //拿到该课程信息
        Course course = courseService.getById(cno);
        QueryWrapper<Score> scoreQueryWrapper = new QueryWrapper<>();
        scoreQueryWrapper.eq("cno", cno);
        //拿到所有选项该课程的学生成绩（不管有没有打分）
        List<Score> scores = scoreService.list(scoreQueryWrapper);
        //丰富列表显示的信息
        ArrayList<StudentWrapper> studentWrappers = new ArrayList<>();
        for(Score score:scores){
            StudentWrapper studentWrapper = new StudentWrapper();
            //取得学生成绩
            studentWrapper.setScore(score);
            //拿到成绩表对应课程名
            studentWrapper.setCourseName(course.getCname());
            QueryWrapper<Student> studentQueryWrapper = new QueryWrapper<>();
            studentQueryWrapper.eq("sno",score.getSno());
            Student one = studentService.getOne(studentQueryWrapper);
            //拿到该成绩对应的学生
            studentWrapper.setStudentName(one.getSname());
            studentWrappers.add(studentWrapper);
        }
        HashMap<String, List<StudentWrapper>> map = new HashMap<>();
        map.put("mystudentScore",studentWrappers);
        return map;
    }

    //教师教的课程
    @GetMapping("getMyTaughtCourse")
    public Map<String,List<Course>>getMyCourse(int tno){
        QueryWrapper<Course> courseQueryWrapper = new QueryWrapper<>();
        courseQueryWrapper.eq("tno",tno);
        List<Course> courses = courseService.list(courseQueryWrapper);
        HashMap<String,  List<Course>> stringCourseHashMap = new HashMap<>();
        stringCourseHashMap.put("myTeachcourse",courses);
        return stringCourseHashMap;
    }
    //老师开课的接口
    @PutMapping("openCourse")
    public ReturnMsg openCourse(int tno,int credit,int max_stuNum,String cname){
        Teacher teacher = teacherService.getById(tno);
        Course course = new Course();
        course.setMajorId(teacher.getMajorId());
        course.setCredit(credit);
        course.setCname(cname);
        course.setStuNumMax(max_stuNum);
        course.setTno(tno);
        ReturnMsg returnMsg = new ReturnMsg();
        //课程表多了一条记录
        if(courseService.save(course)){
            returnMsg.setMsg("开课成功");
            returnMsg.setCode(200);
            return returnMsg;
        }else {
            returnMsg.setMsg("服务器出故障了，请耐心等待专业人员维修，会修才怪");
            returnMsg.setCode(-8);
            return returnMsg;
        }
    }

    //老师更新分数接口
    @PutMapping("giveScore")
    public ReturnMsg updateScore(int cno,int sno,int grade){
        UpdateWrapper<Score> scoreUpdateWrapper = new UpdateWrapper<>();
        scoreUpdateWrapper.eq("cno",cno);
        scoreUpdateWrapper.eq("sno",sno);
        scoreUpdateWrapper.set("grade",grade);
        scoreService.update(scoreUpdateWrapper);
        ReturnMsg returnMsg = new ReturnMsg();
        returnMsg.setMsg("更新成功");
        returnMsg.setCode(200);
        return returnMsg ;
    }

    //老师更改课程信息
    @PutMapping("updateCourse")
    public ReturnMsg updateCourse(Course course){

        courseService.updateById(course);
        ReturnMsg returnMsg = new ReturnMsg();
        returnMsg.setMsg("更新成功");
        returnMsg.setCode(200);
        return returnMsg ;
    }

    //根据院系ID获取老师
    @GetMapping("getTeacherByDeptID")
    public R getTeacherByDeptID(int deptID){
        QueryWrapper<Teacher> teacherQueryWrapper = new QueryWrapper<>();
        teacherQueryWrapper.eq("major_id",deptID);
        return success(this.teacherService.list(teacherQueryWrapper));
    }







}